package com.example.gamecaro;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;


import android.media.ExifInterface;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class Game extends View {
	private Cell [][]ChessBoard=null;
	int x=20;								//chiều ngang
	int y=20;		//chiều dọc
	private Paint paint;
	public boolean whatdraw;
	int with, height;
	
	private int Eval_Board=0;
	private int x_attack=0,y_attack=0;
	public String Player1;
	public String Player2;
	public int type=0;
	public int turning=0;
	Handler handler=new Handler(){
		public void handleMessage(Message msg){
			switch (msg.what) {
			case 0:
				invalidate();
				break;
			case 1:
				Toast.makeText(getContext(),Player1, Toast.LENGTH_SHORT).show();
				break;
			case 2:
				Toast.makeText(getContext(),Player2, Toast.LENGTH_SHORT).show();
				break;
			case 3:
				Toast.makeText(getContext(), "Loose", Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	public Game(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		paint=new Paint();
		this.paint.setARGB(255, 0, 0, 0);
		this.paint.setAntiAlias(true);
		this.paint.setStyle(Style.STROKE);
		this.paint.setStrokeWidth(5);
		with=this.getWidth()/x;
		height=this.getHeight()/y;
		ChessBoard=new Cell[x][y];
		for(int i=0;i<y;i++){
			for(int j=0;j<x;j++){
				ChessBoard[i][j]=new pieces_empty(with, height);
			}
		}
		
		
	
	}
	protected void onDraw(Canvas canvas){
		for(int i=0;i<ChessBoard.length;i++){
			for(int j=0;j<ChessBoard[0].length;j++){
				ChessBoard[i][j].draw(canvas,getResources(), j, i,(this.getWidth())/ChessBoard.length,(this.getHeight())/ChessBoard[0].length);
			}
		}
		super.onDraw(canvas);
	}
	public boolean onTouchEvent(MotionEvent event){
		int point_x=(int)(event.getX()/(this.getWidth()/x));
		int point_y=(int)(event.getY()/(this.getHeight()/y));
		if(type==0)
			drawpieces(point_x,point_y);
		else
			drawai(point_x, point_y, turning);
		return super.onTouchEvent(event);
	}
	private void drawpieces(int point_x,int point_y) {
		// TODO Auto-generated method stub
		Cell cell=null;
		if(whatdraw)
		{
			if(!Empty_Check(point_x, point_y))
				return;
			cell=new pieces_o(ChessBoard[point_x][point_y].x,ChessBoard[point_x][point_y].y);
			whatdraw=false;
		}
		else
		{
			if(!Empty_Check(point_x, point_y))
				return;
			cell=new pieces_x(ChessBoard[point_x][point_y].x,ChessBoard[point_x][point_y].y);
			whatdraw=true;
		}
		ChessBoard[point_y][point_x]=cell;
		handler.sendMessage(Message.obtain(handler,0));
		if(Check_Win_1())
		{
			if(whatdraw)
			{
				invalidate();
				AlertDialog dialog=makeChoise(Player1);
				dialog.show();
			}
			else{
				invalidate();
				AlertDialog dialog=makeChoise(Player2);
				dialog.show();
			}
		}
		else if(isfull()){
			handler.sendMessage(Message.obtain(handler,3));
			resizeGame();
		}
	}
	
	private void drawai(int point_x,int point_y,int turning)
	{
		Cell cell=null;
		if(!Empty_Check(point_x, point_y))
			return;
		if(turning==0)
		{
			cell=new pieces_o(ChessBoard[point_x][point_y].x,ChessBoard[point_x][point_y].y);
			whatdraw=false;
		}
		else
		{
			cell=new pieces_x(ChessBoard[point_x][point_y].x,ChessBoard[point_x][point_y].y);
			whatdraw=true;
		}
		ChessBoard[point_y][point_x]=cell;
		if(Check_Win_1())
		{
			if(whatdraw && turning==0)
			{
				invalidate();
				AlertDialog dialog=makeChoise(Player2);
				dialog.show();
			}
			else if(whatdraw==false && turning==1)
			{
				invalidate();
				AlertDialog dialog=makeChoise(Player2);
				dialog.show();
			}
			else
			{
				invalidate();
				AlertDialog dialog=makeChoise(Player1);
				dialog.show();
			}
			return;
		}else if(isfull()){
			handler.sendMessage(Message.obtain(handler,3));
			resizeGame();
			return;
		}
		AICaro(turning);
		if(turning==1)
		{
			ChessBoard[x_attack][y_attack]=new pieces_o(this.getWidth()/x, this.getHeight()/y);
			whatdraw=false;
		}
		else
		{
			ChessBoard[x_attack][y_attack]=new pieces_x(this.getWidth()/x, this.getHeight()/y);
			whatdraw=true;
		}
		handler.sendMessage(Message.obtain(handler,0));
		if(Check_Win_1())
		{
			if(whatdraw && turning==0)
			{
				invalidate();
				AlertDialog dialog=makeChoise(Player2);
				dialog.show();
			}
			else if(whatdraw==false && turning==1)
			{
				invalidate();
				AlertDialog dialog=makeChoise(Player2);
				dialog.show();
			}
			else
			{
				invalidate();
				AlertDialog dialog=makeChoise(Player1);
				dialog.show();
			}
			return;
		}else if(isfull()){
			handler.sendMessage(Message.obtain(handler,3));
			resizeGame();
			return;
		}
	
	}
	private boolean Check_Win_1(){
		int pieces=0;
		Cell cell=null;
		Cell temp=null;
		int win[]=new int [10];
		int i,j,hang,cot,newCount,x_temp=0,y_temp=0;
		for(hang=0;hang<ChessBoard.length;hang++){
			for(cot=0;cot<ChessBoard[0].length;cot++){
				if(!(ChessBoard[hang][cot] instanceof pieces_empty)){
					cell=ChessBoard[hang][cot];
					if(ChessBoard[hang][cot] instanceof pieces_o)
						temp=new pieces_o_end(this.getWidth()/x, this.getHeight()/y);
					else if(ChessBoard[hang][cot] instanceof pieces_x)
						temp=new pieces_x_end(this.getWidth()/x, this.getHeight()/y);
					//xet hang ngang
					i=hang;
					j=cot;
					while(i>0&&ChessBoard[i-1][j] .equals( cell)){
						i--;
					}
					newCount=0;
					while(i<ChessBoard.length&&ChessBoard[i][j].equals(cell)){
						newCount++;
						win[newCount*2-1-1]=i;
						win[newCount*2-1]=j;
						i++;
						
					}
					if(newCount>=5){
						for(int l=0;l<win.length;l++){
							if(l%2==0)
								x_temp=win[l];
							else if(l%2==1){
								y_temp=win[l];
								ChessBoard[x_temp][y_temp]=temp;
							}
						}
						return true;
					}
					//xet hàng dọc
					i=hang;
					j=cot;
					while(j>0&&ChessBoard[i][j-1].equals(cell)){
						j--;
					}
					newCount=0;
					while(j<ChessBoard.length&&ChessBoard[i][j].equals(cell)){
						newCount++;
						win[newCount*2-1-1]=i;
						win[newCount*2-1]=j;
						j++;
					}
					if(newCount>=5)
					{
						for(int l=0;l<win.length;l++){
							if(l%2==0)
								x_temp=win[l];
							else if(l%2==1){
								y_temp=win[l];
								ChessBoard[x_temp][y_temp]=temp;
							}
						}
						return true;
					}
					//Xet hàng cheo
					i=hang;
					j=cot;
					while(i+1<ChessBoard.length && j+1<ChessBoard.length
							&& ChessBoard[i+1][j+1].equals(cell)){
						i++;
						j++;
					}
					newCount=0;
					while(i>=0 && j>=0&&ChessBoard[i][j].equals(cell)){
						newCount++;
						win[newCount*2-1-1]=i;
						win[newCount*2-1]=j;
						i--;
						j--;
					}
					if(newCount>=5){
						for(int l=0;l<win.length;l++){
							if(l%2==0)
								x_temp=win[l];
							else if(l%2==1){
								y_temp=win[l];
								ChessBoard[x_temp][y_temp]=temp;
							}
						}
						return true;
					}
						
					i=hang;
					j=cot;
					while(i>0&&j+1<ChessBoard.length&&ChessBoard[i-1][j+1].equals(cell)){
						i--;
						j++;
					}
					newCount=0;
					while(i<ChessBoard.length&&j>=0&&ChessBoard[i][j].equals(cell)){
						newCount++;
						win[newCount*2-1-1]=i;
						win[newCount*2-1]=j;
						i++;
						j--;
					}
					if(newCount>=5){
						for(int l=0;l<win.length;l++){
							if(l%2==0)
								x_temp=win[l];
							else if(l%2==1){
								y_temp=win[l];
								ChessBoard[x_temp][y_temp]=temp;
							}
						}
						return true;
					}
				}
			}
		}
		return false;
	}
	private boolean Check_Win() {
		// TODO Auto-generated method stub
		int dem=0;
		Cell temp=null;
		//Kiểm tra ăn dọc
		for(int i=0;i<ChessBoard.length;i++){
			for(int j=0;j<ChessBoard[0].length;j++){
				if (!(ChessBoard[i][j].equals(temp)) || ChessBoard[i][j]instanceof pieces_empty) {
					temp=ChessBoard[i][j];
					dem=0;
				} else {
					dem++;
				}
				if(dem>=4)
					return true;
				
			}
			temp=null;
			dem=0;
		}
		temp=null;
		//kiểm tra ăn ngang
		for(int j=0;j<ChessBoard[0].length;j++){
			for(int i=0;i<ChessBoard.length;i++){
				if(!(ChessBoard[i][j].equals(temp)) || ChessBoard[i][j]instanceof pieces_empty)
				{
					temp=ChessBoard[i][j];
					dem=0;
				}
				else
				{
					dem++;
					System.out.println(dem);
				}
				if(dem>=4)
					return true;
			}
			temp=null;
			dem=0;
		}
		temp=null;
		for(int j=ChessBoard[0].length-1;j>=0;j--){
			int k=0;
			for(int z=j;z<ChessBoard[0].length;z++){
				if(!ChessBoard[k][z].equals(temp)|| ChessBoard[k][z] instanceof pieces_empty){
					temp=ChessBoard[k][z];
					dem=0;
				}
				else
				{
					dem++;
					System.out.println(dem);
				}
				if(dem>=4)
					return true;
				k++;
			}
			temp=null;
			dem=0;
		}
		temp=null;
		for(int j=0;j<ChessBoard[0].length;j++){
			int k=0;
			for(int z=j;z>=0;z--){
				if(!ChessBoard[k][z].equals(temp)|| ChessBoard[k][z] instanceof pieces_empty){
					temp=ChessBoard[k][z];
					dem=0;
				}
				else
					dem++;
				if(dem>=4)
					return true;
				k++;
			}
			temp=null;
			dem=0;
		}
		
		return false;
	}
	private boolean Empty_Check(int point_x,int point_y)
	{
		if(ChessBoard[point_y][point_x]instanceof pieces_empty )
			return true;
		return false;
	}
	private void resizeGame()
	{
		for(int i=0;i<y;i++){
			for(int j=0;j<x;j++){
				ChessBoard[i][j]=new pieces_empty((this.getWidth()/x)*j, (this.getHeight()/y)*i);
			}
		}
		invalidate();
	}
	private void AICaro(int t )
	{
		for(int i=0;i<ChessBoard.length;i++){
			for(int j=0;j<ChessBoard[0].length;j++){
				if(ChessBoard[i][j] instanceof pieces_empty){
					if(t==0)
						ChessBoard[i][j]=new pieces_o(this.getWidth()/x, this.getHeight()/y);
					else
						ChessBoard[i][j]=new pieces_x(this.getWidth()/x, this.getHeight()/y);
					int m=check_colum(t)+check_row(t)+check_cross_left(t)+eval_attack(i,j,t)+eval_attack(i, j, 1-t);
					if(Eval_Board<m)
					{
						Eval_Board=m;
						x_attack=i;
						y_attack=j;
					}
					ChessBoard[i][j]=new pieces_empty(this.getWidth()/x, this.getHeight()/y);
				}
				
			}
		}
		Eval_Board=0;
	}
	private int check_row(int k) {
		// TODO Auto-generated method stub
		int sum=0;
		Cell cell=null;
		if(k==1)
			cell=new pieces_o(this.getWidth()/x, this.getHeight()/y);
		else
			cell=new pieces_x(this.getWidth()/x, this.getHeight()/y);
		
		for(int i=0;i<ChessBoard[0].length;i++){
			int d=1;
			for(int j=0;j<ChessBoard.length;j++){
				if(ChessBoard[j][i].equals(cell)){
					for(int j1=j+1;j1<ChessBoard.length;j1++){
						if(ChessBoard[j1][i].equals(cell)){
							  d++;
							  j=j1+1;
						}
						else
						{
							 sum+=d*d;
							  // Cộng điểm cho trường hợp đặc biệt 4 quân thì 50 và 5 quân thì 100
							  if(d==4)
								  sum+=50;
							  else if(d==5)
								  sum+=100;
							  d=1;
							  j=j1;
							  break;
						}
					}
				}
			}
		}
		return sum;
	}
	private int check_colum(int k) {
		// TODO Auto-generated method stub
		int sum=0;
		Cell cell=null;
		if(k==1)
			cell=new pieces_o(this.getWidth()/x, this.getHeight()/y);
		else
			cell=new pieces_x(this.getWidth()/x, this.getHeight()/y);
		
		for(int i=0;i<ChessBoard.length;i++){
			int d=1;
			for(int j=0;j<ChessBoard[0].length;j++){
				if(ChessBoard[i][j].equals(cell)){
					for(int j1=j+1;j1<ChessBoard[0].length;j1++){
						if(ChessBoard[i][j1].equals(cell)){
							d++;
							j=j1+1;
						}else{
							sum+=d*d;
							  if(d==4)
								  sum+=50;
							  else if(d==5)
								  sum+=100;
							  d=1;
							  j=j1;
							  break;
						}
							
					}
				}
			}
		}
			
		return sum;
	}
	private int check_cross_left(int k) {
		// TODO Auto-generated method stub
		int sum=0;
		Cell cell=null;
		if(k==1)
			cell=new pieces_o(this.getWidth()/x, this.getHeight()/y);
		else
			cell=new pieces_x(this.getWidth()/x, this.getHeight()/y);
		for(int i=0;i<ChessBoard.length;i++){
			for(int j=i;j>=0;j--){
				if(ChessBoard[i][j].equals(cell)){
					int d=1;
					for(int j1=j-1;j1>=0;j1--){
						if(ChessBoard[i-j1][j1].equals(cell)){
							d++;
							j=j1-1;
						}else{
							 sum+=d*d;
							  // Cộng điểm cho trường hợp đặc biệt 4 quân thì 50 và 5 quân thì 100
							  if(d==4)
								  sum+=50;
							  else if(d==5)
								  sum+=100;
							 j=j1;
							 d=0;
							 break;
						}
						sum+=d*d;
					}
				}
			}
		}
		return sum;
	}
	private int check_cross_right(int k) {
		// TODO Auto-generated method stub
		int sum=0;
		Cell cell=null;
		if(k==1)
			cell=new pieces_o(this.getWidth()/x, this.getHeight()/y);
		else
			cell=new pieces_x(this.getWidth()/x, this.getHeight()/y);
		for(int i=0;i<ChessBoard[0].length;i++){
			for(int j=0;j<ChessBoard.length-i;j++){
				if(ChessBoard[i+j][j].equals(cell)){
					int d=1;
					for(int j1=j+1;j1<ChessBoard[0].length;j1++){
						if(ChessBoard[i+j1][j1].equals(cell)){
							  d++;
							  j=j1+1;
						}else{
							  sum+=d*d;
							  // Cộng điểm cho trường hợp đặc biệt 4 quân thì 50 và 5 quân thì 100
							  if(d==4)
								  sum+=50;
							  else if(d==5)
								  sum+=100;
							  d=0;
							  j=j1;
							  break;
						}
						sum+=d*d;
							
					}
				}
			}
			
		}
		return sum;
	}
	private int eval_attack(int i, int j,int k) {
		// TODO Auto-generated method stub
		Cell cell_x=null;
		Cell cell_y=null;
		if(k==1)
		{
			cell_x=new pieces_x(this.getWidth()/x,this.getHeight()/y);
			cell_y=new pieces_o(this.getWidth()/x, this.getHeight()/y);
		}else{
			cell_x=new pieces_o(this.getWidth()/x, this.getHeight()/y);
			cell_y=new pieces_x(this.getWidth()/x, this.getHeight()/y);
		}
		Cell cell_empty=new pieces_empty(this.getWidth()/x, this.getHeight()/y);
		int sum=0;
		if(i>=4&&ChessBoard[i-1][j].equals(cell_y)){
			if(ChessBoard[i-2][j].equals(cell_y)){
				if(ChessBoard[i-3][j].equals(cell_y)){
					if(ChessBoard[i-4][j].equals(cell_y)){
						sum+=150;
					}
					else{
						if(ChessBoard[i-4][j].equals(cell_empty)){
							sum+=50;
						}
						else sum+=3*3;
					}
				}else{
					sum+=2*2;
				}
			}else{
				sum+=1*1;
			}
			
		}
		if(j>=4&&ChessBoard[i][j-1].equals(cell_y)){
			if(ChessBoard[i][j-2].equals(cell_y)){
				if(ChessBoard[i][j-3].equals(cell_y)){
					if(ChessBoard[i][j-4].equals(cell_y)){
						sum+=150;
					}else{
						if(ChessBoard[i][j-4].equals(cell_empty)){
							sum+=50;
						}else{
							sum+=3*3;
						}
					}
				}else{
					sum+=2*2;
				}
			}else{
				sum+=1*1;
			}
		}
		if(i<ChessBoard[0].length-4&&ChessBoard[i+1][j].equals(cell_y)){
			if(ChessBoard[i+2][j].equals(cell_y)){
				if(ChessBoard[i+3][j].equals(cell_y)){
					if(ChessBoard[i+4][j].equals(cell_y)){
						sum+=150;
					}else{
						if(ChessBoard[i+4][j].equals(cell_empty)){
							sum+=50;
						}else{
							sum+=3*3;
						}
					}
				}else{
					sum+=2*2;
				}
			}else{
				sum+=1*1;
			}
		}
		
		if(j<ChessBoard.length-4&&ChessBoard[i][j+1].equals(cell_y)){
			if(ChessBoard[i][j+2].equals(cell_y)){
				if(ChessBoard[i][j+3].equals(cell_y)){
					if(ChessBoard[i][j+4].equals(cell_y)){
						sum+=150;
				}else{
					if(ChessBoard[i][j+4].equals(cell_empty)){
						sum+=50;
					}
					else{
						sum+=3*3;
					}
				}
			}else{
				sum+=2*2;
			}
		}else{
			sum+=1*1;
		}
		}
		
		if(i>=4&&j>=4&&ChessBoard[i-1][j-1].equals(cell_y)){
			if(ChessBoard[i-2][j-2].equals(cell_y)){
				if(ChessBoard[i-3][j-3].equals(cell_y)){
					if(ChessBoard[i-4][j-4].equals(cell_y)){
						sum+=150;
					}else{
						if(ChessBoard[i-4][j-4].equals(cell_empty)){
							sum+=50;
						}else{
							sum+=3*3;
						}
					}
				}else{
					sum+=2*2;
				}
			}else{
				sum+=1*1;
			}
		}
		
		if(i<ChessBoard[0].length-4&&j<ChessBoard.length-4&&ChessBoard[i+1][j+1].equals(cell_y)){
			if(ChessBoard[i+2][j+2].equals(cell_y)){
				if(ChessBoard[i+3][j+3].equals(cell_y)){
					if(ChessBoard[i+4][j+4].equals(cell_y)){
						sum+=150;
					}else{
						if(ChessBoard[i+4][j+4].equals(cell_empty)){
							sum+=50;
						}else{
							sum+=3*3;
						}
							
					}
				}else{
					sum+=2*2;
				}
			}else{
				sum+=1*1;
			}
		}
		if(i>=4&&j<ChessBoard.length-4&&ChessBoard[i-1][j+1].equals(cell_y)){
			if(ChessBoard[i-2][j+2].equals(cell_y)){
				if(ChessBoard[i-3][j+3].equals(cell_y)){
					if(ChessBoard[i-4][j+4].equals(cell_y)){
						sum+=150;
					}else{
						if(ChessBoard[i-4][j+4].equals(cell_y)){
							sum+=50;
						}else{
							sum+=3*3;
						}
					}
				}else{
					sum+=2*2;
				}
			}else{
				sum+=1*1;
			}
		}
		if(i<ChessBoard[0].length-4&&j>=4&&ChessBoard[i+1][j-1].equals(cell_y)){
			if(ChessBoard[i+2][j-2].equals(cell_y)){
				if(ChessBoard[i+3][j-3].equals(cell_y)){
					if(ChessBoard[i+4][j-4].equals(cell_y)){
						sum+=150;
					}else{
						if(ChessBoard[i+4][j-4].equals(cell_empty)){
							sum+=50;
						}else{
							sum+=3*3;
						}
					}
				}else{
					sum+=2*2;
				}
			}else{
				sum+=1*1;
			}
		}
		//chặn 3 nguy hiểm xx x
		if(i>=3&&i<ChessBoard[0].length-2&&ChessBoard[i-2][j].equals(cell_y)&&ChessBoard[i-1][j].equals(cell_y)&&ChessBoard[i+1][j].equals(cell_y)&&
				!ChessBoard[i-3][j].equals(cell_x)&& !ChessBoard[i+2][j].equals(cell_x))
			sum+=150;
		if(i>=2&&i<ChessBoard[0].length-3&&ChessBoard[i-1][j].equals(cell_y)&&!ChessBoard[i-2][j].equals(cell_x)
				&&ChessBoard[i+1][j].equals(cell_y)&&ChessBoard[i+2][j].equals(cell_y)&&!ChessBoard[i+3][j].equals(cell_x))
			sum+=150;
		if(j>=3&&j<ChessBoard.length-2&&ChessBoard[i][j-2].equals(cell_y)&&ChessBoard[i][j-1].equals(cell_y)
				&&ChessBoard[i][j+1].equals(cell_y)&&!ChessBoard[i][j-3].equals(cell_x)
				&&!ChessBoard[i][j+2].equals(cell_x))
			sum+=150;
		if(j>=2&&j<ChessBoard.length-3&&ChessBoard[i][j-1].equals(cell_y)&&!ChessBoard[i][j-2].equals(cell_x)&&ChessBoard[i][j+1].equals(cell_y)
				&& ChessBoard[i][j+2].equals(cell_y)&&!ChessBoard[i][j+3].equals(cell_x))
			sum+=150;
		if(i>=3&&j>=3&&j<ChessBoard.length-2&&i<ChessBoard[0].length-2&&!ChessBoard[i-3][j-3].equals(cell_x)&&ChessBoard[i-2][j-2].equals(cell_y)
				&&ChessBoard[i-1][j-2].equals(cell_y)&&ChessBoard[i+1][j+1].equals(cell_y)&&!ChessBoard[i+2][j+2].equals(cell_x))
			sum+=150;
		if(i>=2&&j>=2&&i<ChessBoard[0].length-3&&j<ChessBoard.length-3&&!ChessBoard[i-2][j-2].equals(cell_x)&&ChessBoard[i-1][j-1].equals(cell_y)
				&&ChessBoard[i+1][j+1].equals(cell_y)&&ChessBoard[i+2][j+2].equals(cell_y)&&!ChessBoard[i+3][j+3].equals(cell_x))
			sum+=150;
		if(i>=3&&i<ChessBoard[0].length-2&&j<ChessBoard.length-3&&j>=2&&!ChessBoard[i-3][j+3].equals(cell_x)&&ChessBoard[i-2][j+2].equals(cell_y)
				&&ChessBoard[i-1][j+1].equals(cell_y)&&ChessBoard[i+1][j-1].equals(cell_y)&&!ChessBoard[i+2][j-2].equals(cell_x))
			sum+=150;
		if(i>=2&&i<ChessBoard[0].length-3&&j<ChessBoard.length-2&&j>=3&&!ChessBoard[i-2][j+2].equals(cell_x)&&ChessBoard[i-1][j+1].equals(cell_y)
				&&ChessBoard[i+1][j-1].equals(cell_y)&&ChessBoard[i+2][j-2].equals(cell_y)&&!ChessBoard[i+3][j-3].equals(cell_x))
			sum+=150;
		//
		if(i<ChessBoard[0].length-4&&ChessBoard[i+1][j].equals(cell_y)&&ChessBoard[i+2][j].equals(cell_y)&&ChessBoard[i+3][j].equals(cell_y)&&!ChessBoard[i+4][j].equals(cell_x))
			sum+=150;
		if(j<ChessBoard.length-4&&ChessBoard[i][j+1].equals(cell_y)&&ChessBoard[i][j+2].equals(cell_y)&&ChessBoard[i][j+3].equals(cell_y)&&!ChessBoard[i][j+4].equals(cell_x))
			sum+=150;
		if(i<ChessBoard[0].length-4&&j<ChessBoard.length-4&&ChessBoard[i+1][j+1].equals(cell_y)&&ChessBoard[i+2][j+2].equals(cell_y)&&ChessBoard[i+3][j+3].equals(cell_y)&&!ChessBoard[i+4][j+4].equals(cell_x))
			sum+=150;
		if(i<ChessBoard.length-4&&j>=4&&ChessBoard[i+1][j-1].equals(cell_y)&&ChessBoard[i+2][j-2].equals(cell_y)&&ChessBoard[i+3][j-3].equals(cell_y)&&!ChessBoard[i+4][j-4].equals(cell_x))
			sum+=150;
		return sum;
   }
	private AlertDialog makeChoise(String S){
		AlertDialog myDialog=new AlertDialog.Builder(getContext()).setTitle(S).setMessage("Are you want continue this game again").setIcon(R.drawable.ic_launcher)
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						resizeGame();
					}
				})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Intent menu=new Intent("com.example.gamecaro.main");
						menu.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						getContext().startActivity(menu);
					
						
					}
				})
				.create();
		return myDialog;	
	}
	private boolean isfull(){
		for(int i=0;i<ChessBoard.length;i++){
			for(int j=0;j<ChessBoard[0].length;j++){
				if(ChessBoard[i][j] instanceof pieces_empty)
					return false;
			}
		}
		return  true;
		
	}
}
